<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索 -->
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item>
          <el-input placeholder="请输入内容" clearable v-model="listQuery.name">
            <el-button slot="append" icon="el-icon-search" @click="getBidType">
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
          >
            新增类型
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table
        style="width: 100%"
        :data="bidTypeList"
        v-loading="listLoading"
        highlight-current-row
        border
        size="small"
      >
        <el-table-column label="#" align="center" type="index" width="50">
        </el-table-column>
        <el-table-column prop="id" align="center" label="类型ID">
        </el-table-column>
        <el-table-column prop="name" align="center" label="类型名称">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话框 -->
    <el-dialog
      :title="edit ? '修改类型' : '新增标书类型'"
      :visible.sync="addDialogVisible"
      width="50%"
      @closed="handleCloseDialog"
    >
      <el-form
        :model="addBidTypeForm"
        :rules="addBidTypeFormRules"
        ref="addBidTypeFormRef"
        label-width="80px"
        label-position="right"
        style="width: 95%"
      >
        <el-form-item label="类型ID" prop="id">
          <el-input
            v-model="addBidTypeForm.id"
            placeholder="类型ID"
            maxlength="64"
            clearable
            :disabled="edit"
          />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input
            v-model="addBidTypeForm.name"
            placeholder="类型名称"
            maxlength="64"
            clearable
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit ? updateBidType() : addBidType()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
const defaultBidType = {
  id: "",
  name: "",
};
import {
  bidType_list,
  addBidType,
  delBidType,
  editBidType,
  updateBidType,
} from "@/api/building";
export default {
  name: "bid_type",
  data() {
    return {
      editId: "",
      title: "",
      edit: false,
      bidTypeList: [],
      listLoading: true,
      listQuery: {
        id: "",
        name: "",
        page: 1,
        page_size: 20,
      },
      total: 0,
      dialogVisible: false,
      bidTypeForm: Object.assign({}, defaultBidType),
      bidTypeFormRules: {
        id: [{ required: true, message: "请输入类型ID", trigger: "blur" }],
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
      },
      addDialogVisible: false,
      addBidTypeFormRules: {
        id: [{ required: true, message: "请输入类型ID", trigger: "blur" }],
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
      },
      addBidTypeForm: {
        id: "",
        name: "",
      },
      editBidTypeForm: {},
    };
  },
  created() {
    this.getBidType();
  },
  methods: {
    //获取数据列表
    getBidType() {
      this.listLoading = true;
      bidType_list(this.listQuery)
        .then((res) => {
          this.total = res.length;
          this.bidTypeList = res;
          this.listLoading = false;
        })
        .catch((error) => {
          console.log(error.message);
          this.listLoading = false;
        });
    },

    //点击添加标书类型
    handleAdd() {
      this.edit = false;
      this.addDialogVisible = true;
    },
    //确定添加类型
    addBidType() {
      this.$refs.addBidTypeFormRef.validate((valid) => {
        if (!valid) {
          this.$message.error("请填写必填项");
          this.addBidTypeForm = {};
          return;
        } else {
          addBidType(this.addBidTypeForm)
            .then((res) => {
              if (res) {
                this.addDialogVisible = false;
                this.$message.success("添加成功！");
                this.addBidTypeForm = {};
                this.getBidType();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    //删除类型
    async handleDelete(id) {
      const removeResult = await this.$confirm("是否确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (removeResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      delBidType(id).then((res) => {
        this.$message.success("删除成功！");
        this.getBidType();
      });
    },
    //数据回显，弹框显示
    handleEdit(bidTypeInfo) {
      this.edit = true;
      editBidType(bidTypeInfo).then((res) => {
        this.addBidTypeForm = res;
      });
      this.addDialogVisible = true;
    },
    //更新数据，修改完成
    async updateBidType() {
      this.$refs.addBidTypeFormRef.validate(async (valid) => {
        if (!valid) return;
        else {
          const res = await updateBidType(this.addBidTypeForm);
          const { id } = res;
          if (id > 0) {
            this.getBidType();
            this.$message.success("修改成功");
            this.addDialogVisible = false;
          }
        }
      });
    },
    //关闭编辑对话框
    handleCloseDialog() {
      this.addBidTypeForm = {};
      this.addDialogVisible = false;
      this.$refs.addBidTypeFormRef.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>